//
//  StringHelper.m
//  DeyiLife
//
//  Created by zxin on 15/7/10.
//  Copyright (c) 2015年 Deyi. All rights reserved.
//

#import "StringHelper.h"

@implementation StringHelper

+ (NSString *)stringFromHexString:(NSString *)hexString {
    
    char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
    bzero(myBuffer, [hexString length] / 2 + 1);
    for (unsigned int i = 0; i < [hexString length] - 1; i += 2) {
        unsigned int anInt;
        NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
        NSScanner * scanner = [[NSScanner alloc] initWithString:hexCharStr];
        [scanner scanHexInt:&anInt];
        myBuffer[i / 2] = (char)anInt;
    }
    NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
    free(myBuffer);
    return unicodeString;
}

+ (NSString *)hexStringFromString:(NSString *)string {
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    
    NSString *hexStr=@"";
    for(unsigned int i=0; i < [myD length]; i++) {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];
        if([newHexStr length] == 1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}

+ (NSString *)makeAmrFilename:(NSString *)amrUrl {
    NSString *filename = [amrUrl stringByReplacingOccurrencesOfString:@"http://img.deyi.net/forum/" withString:@""];
    filename = [filename stringByReplacingOccurrencesOfString:@"/" withString:@""];
    return filename;
}

+ (NSString *)stringbyFilterString:(NSString *)string
{
    NSString *returnString = string;
    returnString = [returnString stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br>"];
    returnString = [returnString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
    returnString = [returnString stringByReplacingOccurrencesOfString:@"\r" withString:@"<br>"];
    //  returnString = [returnString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    //   returnString = [returnString stringByReplacingOccurrencesOfString:@"/" withString:@""];
    
    return returnString;
}

+ (NSString *)replaceUnicode:(NSString *)unicodeStr {
    if (!unicodeStr) {
        return @"";
    }
    NSString *tempStr1 =
    [unicodeStr stringByReplacingOccurrencesOfString:@"\\u"
                                                 withString:@"\\U"];
    NSString *tempStr2 =
    [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *tempStr3 =
    [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString *str =
    [NSPropertyListSerialization propertyListFromData:tempData
                                     mutabilityOption:NSPropertyListImmutable
                                               format:NULL
                                     errorDescription:NULL];
    return str;
}

@end
